package com.sxj.corejava.code08_常用类.object;

import java.util.Objects;

/**
 * @author 石小俊
 */
public class Person implements Cloneable{

    private String name;
    private int age;
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    //    @Override
//    public boolean equals(Object obj) {
//        // 判断当前参数的类型是否与当前对象类型一致
//        if (obj.getClass() == Person.class) {
//            // 如果一致,则将参数对象强制转换成当前对象的类型
//            Person person = (Person) obj;
//            // 当参数对象的所有属性的值与当前对象的所有属性的值完全一致时,表示两者是同一个对象
//            return this.name.equals(person.getName()) && this.age == person.getAge();
//        }
//        // 如果不一致,直接返回false
//        return false;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

//    @Override
//    public String toString() {
//        return "name:" + name + ",age:" + age;
//    }


    @Override
    protected void finalize() throws Throwable {
        System.out.println("正在销毁");
        super.finalize();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
